package com.sh.weblogspringboot.web.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists;
import com.sh.weblogspringboot.common.domain.dos.ArticleCategoryRelDO;
import com.sh.weblogspringboot.common.domain.dos.ArticleDO;
import com.sh.weblogspringboot.common.domain.dos.CategoryDO;
import com.sh.weblogspringboot.common.domain.mapper.ArticleCategoryRelMapper;
import com.sh.weblogspringboot.common.domain.mapper.ArticleMapper;
import com.sh.weblogspringboot.common.domain.mapper.CategoryMapper;
import com.sh.weblogspringboot.common.enums.ResponseCodeEnum;
import com.sh.weblogspringboot.common.exception.BizException;
import com.sh.weblogspringboot.common.util.PageResponse;
import com.sh.weblogspringboot.common.util.Response;
import com.sh.weblogspringboot.web.convert.ArticleConvert;
import com.sh.weblogspringboot.web.model.vo.FindCategoryListRspVO;
import com.sh.weblogspringboot.web.model.vo.category.FindCategoryArticlePageListReqVO;
import com.sh.weblogspringboot.web.model.vo.category.FindCategoryArticlePageListRspVO;
import com.sh.weblogspringboot.web.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Collection;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @author songHai
 * @Classname CategoryServiceImpl
 * @Description
 * @Date 2025/4/12 1:05
 */
@Service
@Slf4j
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private ArticleCategoryRelMapper articleCategoryRelMapper;

    @Autowired
    private ArticleMapper articleMapper;

    @Override
    public Response findCategoryList() {
        List<CategoryDO> categoryDOList = categoryMapper.selectList(Wrappers.emptyWrapper());
        if (CollectionUtils.isEmpty(categoryDOList)) {
            return Response.success("查询成功", Lists.newArrayList());
        }

        List<FindCategoryListRspVO> vos = categoryDOList.stream().map(categoryDO -> FindCategoryListRspVO.builder().id(categoryDO.getId()).name(categoryDO.getName()).build()).collect(Collectors.toList());

        return Response.success("查询成功", vos);
    }

    @Override
    public Response findCategoryArticlePageList(FindCategoryArticlePageListReqVO findCategoryArticlePageListReqVO) {
        Long current = findCategoryArticlePageListReqVO.getCurrent();
        Long size = findCategoryArticlePageListReqVO.getSize();
        Long categoryId = findCategoryArticlePageListReqVO.getId();

        CategoryDO categoryDO = categoryMapper.selectById(categoryId);
        if (Objects.isNull(categoryDO)) {
            log.warn("==> 该分类不存在，categoryId: {}", categoryId);
            throw new BizException(ResponseCodeEnum.CATEGORY_NOT_EXISTED);
        }

        // 查询该分类所有关联的文章ID
        List<ArticleCategoryRelDO> articleCategoryRelDOS = articleCategoryRelMapper.selectListByCategoryId(categoryId);
        // 如果该分类下没有任何发布文章
        if (CollectionUtils.isEmpty(articleCategoryRelDOS)) {
            log.info("==> 该分类下还未发布任何文章, categoryId: {}", categoryId);
            return Response.success("查询成功", Optional.empty());
        }

        List<Long> articleIds = articleCategoryRelDOS.stream().map(ArticleCategoryRelDO::getArticleId).collect(Collectors.toList());
        // 根据文章ID集合查询文章分页信息
        Page<ArticleDO> page = articleMapper.selectPageListByArticleIds(current, size, articleIds);
        List<ArticleDO> articleDOList = page.getRecords();

        // DO转 VO
        List<FindCategoryArticlePageListRspVO> vos = Lists.newArrayList();
        if (!CollectionUtils.isEmpty(articleDOList)) {
            vos = articleDOList.stream().map(ArticleConvert.INSTANCE::convertDO2FindCategoryPageListVO).collect(Collectors.toList());
        }

        return PageResponse.success(page, vos);
    }
}
